package com.sfh.cpc.home.fragment.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.graphics.Palette;
import android.view.View;
import android.widget.ImageView;

import com.sfh.cpc.R;
import com.sfh.cpc.SearchActivity;
import com.sfh.cpc.com.BaseAppContent;
import com.sfh.cpc.com.ImageLoaderEngine;
import com.sfh.cpc.com.entity.ENote;
import com.sfh.cpc.home.model.HomeViewModel;
import com.sfh.cpc.note.NoteActivity;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;
import com.sfh.lib.ui.AbstractLifecycleFragment;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoaderInterface;

import java.util.List;

/**
 * 功能描述: 推荐
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/6
 */
public class NoteRecommendFragmentView extends AbstractLifecycleFragment<HomeViewModel> {

    public static Fragment newFragment(int index) {
        Fragment fragment = new NoteRecommendFragmentView();
        Bundle data = new Bundle();
        data.putInt("INDEX", index);
        fragment.setArguments(data);
        return fragment;
    }

    @Override
    public String getName() {
        return "推荐";
    }

    private Banner ivTopImage;
    private  List<ENote> mData;

    @Override
    public int getLayout() {
        return R.layout.home__note_recome_fragment;
    }

    @Override
    public void initData(View view) {
        ivTopImage = view.findViewById(R.id.iv_top_image);

        ivTopImage.setImageLoader(new ImageLoaderInterface() {

            @Override
            public void displayImage(Context context, Object path, View imageView) {
                final ImageView bg = imageView.findViewById(R.id.iv_top_image_bg);
                ImageView iv = imageView.findViewById(R.id.iv_top_image);
                String url =  BaseAppContent.getImageUrl(((ENote)path).getImages().get(0));
                new ImageLoaderEngine(getContext(), url).roundTransformCenterCrop(7).into(iv, new ImageLoaderEngine.ImageCallback() {
                    @Override
                    public void onSuccess(BitmapDrawable bitmap) {
                        paletteBitmap(bg, bitmap.getBitmap());
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
            }

            @Override
            public View createImageView(Context context) {
                return View.inflate(context, R.layout.home__note_recome_view, null);
            }
        });
        ivTopImage.setBannerStyle(BannerConfig.NOT_INDICATOR).
                isAutoPlay(true)
                .setDelayTime(5000)
                .setIndicatorGravity(BannerConfig.CENTER)
                .setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                NoteActivity.startUI(getContext(), mData.get(position));
            }
        }).start();

        view.findViewById(R.id.tv_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SearchActivity.start(getActivity());
            }
        });

        final int index = getArguments().getInt("INDEX", 0);
        getViewModel().queryHomeRecomNote(index);
    }


    @LiveDataMatch
    public void onNoteListSuccess(List<ENote> data) {

        if (data != null) {
            mData = data;
            ivTopImage.update(mData);
        }
    }

    /**
     * 异步抽取图片色调方法
     */
    private void paletteBitmap(final ImageView iv, @NonNull Bitmap bitmap) {
        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
            //异步抽取图片色调方法
            @Override
            public void onGenerated(@NonNull Palette palette) {
                //获取柔和的色调
                Palette.Swatch mutedSwatch = palette.getMutedSwatch();
                if (mutedSwatch != null) {
                    iv.setBackgroundColor(mutedSwatch.getRgb());
                }
            }
        });
    }

}
